package com.bawei.hgshop.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bawei.hgshop.entity.MsgResult;
import com.bawei.hgshop.entity.MyPage;
import com.bawei.hgshop.entity.Spec;
import com.bawei.hgshop.entity.SpecOption;
import com.bawei.hgshop.service.SpecService;
import com.github.pagehelper.PageInfo;

/**
 * 
 * @时间  2021-1-15  8:44:27
 *
 */
@RequestMapping("spec")
@Controller
public class SpecController { 
	
	/**
	 * 
	 */
	@Reference
	SpecService specService;

	/**
	 * 
	 * @param request
	 * @param page
	 * @return
	 */
	@RequestMapping("list")
	public String list(HttpServletRequest request,
			MyPage page	) {
		PageInfo<Spec> pageInfo = specService.list(page.getPage(), page.getPageSize());
		request.setAttribute("pageInfo", pageInfo);
		return "spec/list";
		
	}
	
	/**
	 * 跳转到添加的页面
	 * @return
	 */
	@RequestMapping("toadd")
	public String toadd() {
		return "spec/add";
	}
	
	/**
	 * 跳转到修改的页面
	 * @return
	 */
	@RequestMapping("toupdate")
	public String toupdate(HttpServletRequest request,int id) {
		Spec spec = specService.findById(id);
		request.setAttribute("spec", spec);
		return "spec/update";
	}
	
	/**
	 * 
	 * @param spec
	 * @return
	 */
	@RequestMapping("add")
	@ResponseBody
	public MsgResult add(Spec spec) {
		List<SpecOption> options = spec.getOptions();
		// 清洗数据
		for (int i = options.size()-1; i >=0 ; i--) {
			SpecOption option = options.get(i);
			// 如果这个数据不合法则删除
			if(!StringUtils.hasText(option.getOptionName()))
				options.remove(i);
			
		}
		specService.add(spec);
		return new MsgResult(0, "", null);
	}
	
	/**
	 * 
	 * @param spec
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public MsgResult udpate(Spec spec) {
		List<SpecOption> options = spec.getOptions();
		// 清洗数据
		for (int i = options.size()-1; i >=0 ; i--) {
			SpecOption option = options.get(i);
			// 如果这个数据不合法则删除
			if(!StringUtils.hasText(option.getOptionName()))
				options.remove(i);
			
		}
		
		specService.update(spec);
		return new MsgResult(0, "", null);
	}
	
	/**
	 * 删除一个规格选项
	 * @param id
	 * @return
	 */
	@RequestMapping("delOption")
	@ResponseBody
	public MsgResult delOption(int id) {
		specService.delOption(id);
		return new MsgResult(0, "", null);
	}
	
	//
	/**
	 *  批量删除
	 * @return
	 */
	@RequestMapping("delbatch")
	@ResponseBody
	public MsgResult delBatch(HttpServletRequest request,@RequestParam(value = "ids[]") int[] ids) {
		int n  = specService.delBatch(ids);
		//删除成功
		if(n>0)
			return new MsgResult(0, "", null);
		else
			return new MsgResult(2, "删除失败，请稍后再试", null);
	}
	
	@RequestMapping("listOptionsBySpecId")
	@ResponseBody
	public MsgResult listOptionsBySpecId(int specId) {
		List<SpecOption> options = specService.listOptionsBySpecId(specId);
		return new MsgResult(0,"ok",options);
		
	}
	
	
}
